home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  25.4 KB  |  1,066 lines

  1. /* This contains functions to send and receive data from the server */
  2. /* ---------------------------------------------------------------- */
  3.  
  4. #include "headers.h" /* has all important stuff */
  5.  
  6. /* send data to server */
  7. void send_data(char *fmt, ...)
  8. {
  9.    int res;
  10.    int ch = 0;
  11.    int size = 2;
  12.  
  13. #  ifndef NOSSL
  14.    int sslerr = 0;
  15.    int dobreak = 0;
  16. #  endif
  17.  
  18.    va_list args;
  19.  
  20.    char *lnptr, *cmdptr;
  21.    char writebuf[MAXWRITESIZE];
  22.    
  23.    memset(writebuf, 0, sizeof(writebuf));
  24.    memset(logmsg.cmdmsg, 0, MAXSEGSIZE);
  25.    
  26.    va_start(args, fmt);
  27.    (void)vsnprintf(writebuf, sizeof(writebuf) - 1, fmt, args);
  28.    va_end(args);
  29.    
  30.    strncpy(logmsg.cmdmsg, writebuf, MAXSEGSIZE - 1);
  31.    logmsg.cmdlen = strlen(logmsg.cmdmsg);
  32.  
  33.    if (child == 1)
  34.       if (getpid() == chpid) ch = 1;
  35.  
  36.    if (((writebuf[0] == '\n') || (writebuf[0] == '\0')) && 
  37.        (isprint((int)writebuf[1]) == 0)) 
  38.    {
  39. #     ifdef NOSSL
  40.       if (silent != 1)
  41.       {
  42.          if (child == 1)
  43.             debug("[in send_data] (in %s)\n"
  44.                   "the data to send is bad.. returning\n\n",
  45.                   (ch == 1 ? "child" : "parent"));
  46.  
  47.          else
  48.             debug("(in send_data) the data to send is bad.. returning\n\n");
  49.       
  50.          debug("the bad data is:\n%s\n", writebuf);
  51.       }
  52.  
  53. #     else
  54.       error("server possibly disconnected... restarting\n\n");
  55.  
  56.       connected = 0;
  57.  
  58.       if (gotInfoServ == 1) longjmp(reconnect, 1);
  59.       else longjmp(infoconn, 1);
  60. #     endif
  61.  
  62.       return;
  63.    }
  64.  
  65.    if ((didspool == 1) && (strncmp(writebuf, "STREAM :", 8) == 0))
  66.    {   
  67.       if (silent != 1)
  68.       {
  69.          if ((debugging == 1) && (silent != 1))
  70.          {
  71.             (void)putchar('\n');
  72.             (void)write(dblogfd, "\n", 1);
  73.          }
  74.  
  75.          if (strlen(writebuf) <= 11)
  76.             debug("sending the following spooled data to server: %s\n",
  77.                   writebuf);
  78.  
  79.          else
  80.             debug("sending the following spooled data to server:\n%s\n",
  81.                   writebuf);
  82.       }
  83.    }
  84.  
  85.    else 
  86.    {
  87.       if (silent != 1)
  88.       {
  89.          if (strlen(writebuf) <= 9)
  90.          {
  91.             if (child == 1)
  92.                debug("(in %s) sending the following to the server: %s\n", 
  93.                      (ch == 1 ? "child" : "parent"), writebuf);
  94.  
  95.             else debug("sending the following to the server: %s\n", writebuf);
  96.          }
  97.  
  98.          else
  99.          {
  100.             if (child == 1)
  101.                debug("(in %s) sending the following to the server:\n%s\n", 
  102.                      (ch == 1 ? "child" : "parent"), writebuf);
  103.  
  104.             else
  105.                debug("sending the following to the server:\n%s\n", writebuf);
  106.          }
  107.       }
  108.    }
  109.  
  110.    errno = errors = 0;
  111.  
  112.    lnptr = exportInt(logmsg.cmdlen);
  113.    size = 2;
  114.  
  115.    while(1)
  116.    {
  117. #     ifndef NOSSL
  118.       res = SSL_write(sslconn, lnptr, (u_int)size);
  119. #     else
  120.       res = write(sockfd, lnptr, size);      
  121. #     endif
  122.  
  123.       /* ------------------ */
  124.  
  125. #     ifndef NOSSL /* SSL error handling */
  126.       sslerr = SSL_get_error(sslconn, res);
  127.       switch (sslerr)
  128.       {
  129.          case SSL_ERROR_NONE:
  130.             if (size == res)
  131.             {
  132.                dobreak = 1;
  133.                break;
  134.             }
  135.  
  136.             lnptr += res, size -= res;
  137.             break;
  138.  
  139.          case SSL_ERROR_WANT_WRITE:
  140.          case SSL_ERROR_WANT_READ:
  141.          case SSL_ERROR_WANT_X509_LOOKUP:
  142.             errors = 1, dobreak = 1;
  143.             break;
  144.  
  145.          case SSL_ERROR_SYSCALL:
  146.          case SSL_ERROR_SSL:
  147.             errors = 1, dobreak = 1;
  148.             break;
  149.  
  150.          case SSL_ERROR_ZERO_RETURN:
  151.             errors = 1, dobreak = 1;
  152.             break;
  153.       }
  154.  
  155.       if (dobreak == 1)
  156.       {
  157.          dobreak = 0;
  158.          break;
  159.       }
  160.  
  161. #     else /* normal error handling */
  162.       if (res <= 0)
  163.       {
  164.          if ((res == ERROR) && (errno = EINTR)) continue;
  165.  
  166.          errors = 1;
  167.          break;
  168.       }
  169.  
  170.       else if (res < size) 
  171.       {
  172.          lnptr += res, size -= res;
  173.          continue;
  174.       }
  175.  
  176.       else break;
  177. #     endif /* SSL/NOSSL error handling */
  178.    }
  179.  
  180.    if (errors != 1)
  181.    {
  182.       size = logmsg.cmdlen;
  183.       cmdptr = logmsg.cmdmsg;
  184.  
  185.       while(1)
  186.       {
  187. #        ifndef NOSSL
  188.          res = SSL_write(sslconn, cmdptr, (u_int)size);
  189. #        else
  190.          res = write(sockfd, cmdptr, size);
  191. #        endif
  192.  
  193.          /* ------------------ */
  194.  
  195. #        ifndef NOSSL /* SSL error handling */
  196.          sslerr = SSL_get_error(sslconn, res);
  197.          switch (sslerr)
  198.          {
  199.             case SSL_ERROR_NONE:
  200.                if (size == res)
  201.                {
  202.                   dobreak = 1;
  203.                   break;
  204.                }
  205.  
  206.                cmdptr += res, size -= res;
  207.                break;
  208.  
  209.             case SSL_ERROR_WANT_WRITE:
  210.             case SSL_ERROR_WANT_READ:
  211.             case SSL_ERROR_WANT_X509_LOOKUP:
  212.                errors = 1, dobreak = 1;
  213.                break;
  214.  
  215.             case SSL_ERROR_SYSCALL:
  216.             case SSL_ERROR_SSL:
  217.                errors = 1, dobreak = 1;
  218.                break;
  219.  
  220.             case SSL_ERROR_ZERO_RETURN:
  221.                errors = 1, dobreak = 1;
  222.                break;
  223.          }
  224.  
  225.          if (dobreak == 1)
  226.          {
  227.             dobreak = 0;
  228.             break;
  229.          }
  230.  
  231. #        else /* normal error handling */
  232.          if (res <= 0)
  233.          {
  234.             if ((res == ERROR) && (errno = EINTR)) continue;
  235.  
  236.             errors = 1;
  237.             break;
  238.          }
  239.  
  240.          else if (res < size)
  241.          {
  242.             cmdptr += res, size -= res;
  243.             continue;
  244.          }
  245.  
  246.          else break;
  247. #        endif /* SSL/NOSSL error handling */
  248.       }
  249.    }
  250.  
  251. #  ifndef NOSSL /* SSL error handling */
  252.    switch (sslerr)
  253.    {
  254.       case SSL_ERROR_NONE:
  255.          /* we don't need to do anything for this */
  256.          break;
  257.  
  258.       case SSL_ERROR_WANT_WRITE:
  259.       case SSL_ERROR_WANT_READ:
  260.       case SSL_ERROR_WANT_X509_LOOKUP:
  261.          error("got SSL_ERROR_WANT_(WRITE/READ/X509_LOOKUP).. ignoring\n\n");
  262.  
  263.          errors = 0;
  264.          break;
  265.  
  266.       case SSL_ERROR_SYSCALL:
  267.       case SSL_ERROR_SSL:
  268.          if (errno <= 0)
  269.          {
  270.             errors = 0;
  271.             break;
  272.          }
  273.  
  274.          error("error with SSL_write: %s\n\n", strerror(errno));
  275.  
  276.          ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  277.          if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  278.  
  279.          debug("now restarting..\n");
  280.  
  281.          if (done == 1) return;
  282.  
  283.          connected = 0;
  284.  
  285.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  286.          else longjmp(infoconn, 1);
  287.  
  288.       case SSL_ERROR_ZERO_RETURN:
  289.          error("server disconnected.. now restarting\n\n");
  290.  
  291.          if (done == 1) return;
  292.   
  293.          connected = 0;
  294.  
  295.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  296.          else longjmp(infoconn, 1);
  297.    }
  298.  
  299. #  else /* normal error handling */
  300.    if (res <= 0)
  301.    {
  302.       if (done == 1)
  303.       {
  304.          errors = 1;
  305.          return;
  306.       }
  307.  
  308.       if (res == 0)
  309.       {
  310.          connected = 0;
  311.  
  312.          if (child == 1)
  313.             error("(in %s) server disconnected... %s\n\n", 
  314.                   (ch == 1 ? "child" : "parent"), 
  315.                   (ch == 1 ? "aborting" : "restarting"));
  316.  
  317.          else error("server disconnected... restarting\n\n");
  318.  
  319.          if ((child == 1) && (ch == 1)) quit(ERROR);
  320.          else
  321.          {
  322.             signal(SIGPIPE, SIG_IGN);
  323.  
  324.             if (done == 1) return;
  325.  
  326.             connected = 0;
  327.  
  328.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  329.             else longjmp(infoconn, 1);
  330.          }
  331.       }
  332.  
  333.       else if (errno > 0)
  334.       {
  335.          if (child == 1)
  336.          {
  337. #           ifndef NOSSL
  338.             ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  339.             if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  340.  
  341. #           else
  342.             error("(in %s) error sending data to the server: %s\n\n",
  343.                   (ch == 1 ? "child" : "parent"), strerror(errno));
  344. #           endif
  345.          }
  346.  
  347.          else
  348.          {
  349. #           ifndef NOSSL
  350.             ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  351.             if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  352.  
  353. #           else
  354.             error("error sending data to the server: %s\n\n", strerror(errno));
  355. #           endif
  356.          }
  357.  
  358.          if ((child == 1) && (ch == 1)) quit(ERROR);
  359.          else
  360.          {
  361.             signal(SIGPIPE, SIG_IGN);
  362.  
  363.             debug("error with server.. restarting\n\n");
  364.  
  365.             if (done == 1) return;
  366.  
  367.             connected = 0;
  368.  
  369.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  370.             else longjmp(infoconn, 1);
  371.          }
  372.       }
  373.  
  374.       else if ((gotAlrm == 1) || (errors == 1))
  375.       {
  376.          errors = 1, gotAlrm = 0;
  377.  
  378. #        ifndef NOSSL
  379.          ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  380.          if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  381.  
  382. #        else
  383.          error("(in recv_data) timeout or error occured in read()\n\n");
  384. #        endif
  385.  
  386.          debug("now restarting\n\n");
  387.  
  388.          if (done == 1) return;
  389.  
  390.          connected = 0;
  391.  
  392.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  393.          else longjmp(infoconn, 1);
  394.       }
  395.    }
  396. #  endif /* SSL/NOSSL error handling */
  397.  
  398. /*
  399.    if (strncmp(writebuf, "OKAY", 4) != 0)
  400.    {
  401.       if (gotAlrm == 1) error("(in recv_data) gotAlrm = 1\n\n");
  402.  
  403.       signal(SIGALRM, okayTimeout);
  404.       (void)alarm(MAXPAUSE * 3);
  405.  
  406.       * silent = 1, * getOkay(), silent = 0;
  407.       (void)alarm(0);
  408.  
  409.       if ((errors == 1) || (gotAlrm == 1))
  410.       {
  411.          errors = 1, gotAlrm = 0;
  412.  
  413.          if (done == 1) return;
  414.          else if ((child == 1) && (ch == 1)) quit(ERROR);
  415.          else
  416.          {
  417.             signal(SIGPIPE, SIG_IGN);
  418.  
  419.             debug("error with server.. restarting\n\n");
  420.  
  421.             if (done == 1) return;
  422.  
  423.             connected = 0;
  424.  
  425.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  426.             else longjmp(infoconn, 1);
  427.          }
  428.       }
  429.    }
  430. */
  431.  
  432. }
  433.  
  434.    
  435. /* -------------------------------------- */
  436.    
  437.  
  438. /* gets new data from server */
  439. void recv_data(char *readbuf, int len)
  440. {
  441.    int res;
  442.    int size = 2;
  443.  
  444. #  ifndef NOSSL
  445.    int sslerr = 0;
  446.    int dobreak = 0;
  447. #  endif
  448.  
  449.    int ch = 0;      /* 0 == not child, 1 == child */
  450.    int newdata = 0; /* indicate new data when oldbuf is null */
  451.  
  452.    char length[2];
  453.    char *lnptr, *cmdptr;
  454.  
  455.    if (getpid() == chpid) ch = 1;
  456.  
  457.    if (silent != 1)
  458.    {
  459.       if (child == 1)
  460.          debug("(in %s) receiving data...\n", (ch == 1 ? "child" : "parent"));
  461.  
  462.       else debug("receiving data...\n");
  463.    }
  464.  
  465.    /* ------------------- */
  466.    errno = errors = 0;
  467.  
  468.    if ((ch == 0) || (child == 0)) /* in parent */
  469.    {
  470.       errno = 0;
  471.       memset(readbuf, 0, len);
  472.  
  473.       lnptr = length, cmdptr = readbuf;
  474.  
  475.       size = 2;
  476.       while(1)
  477.       {
  478. #        ifndef NOSSL
  479.          res = SSL_read(sslconn, lnptr, (u_int)size);
  480. #        else
  481.          res = read(sockfd, lnptr, size);
  482. #        endif
  483.  
  484.          /* ------------------ */
  485.  
  486. #        ifndef NOSSL /* SSL error handling */
  487.          sslerr = SSL_get_error(sslconn, res);
  488.          switch (sslerr)
  489.          {
  490.             case SSL_ERROR_NONE:
  491.                if (size == res)
  492.                {
  493.                   dobreak = 1;
  494.                   break;
  495.                }
  496.  
  497.                lnptr += res, size -= res;
  498.                break;
  499.  
  500.             case SSL_ERROR_WANT_WRITE:
  501.             case SSL_ERROR_WANT_READ:
  502.             case SSL_ERROR_WANT_X509_LOOKUP:
  503.                errors = 1, dobreak = 1;
  504.                break;
  505.  
  506.             case SSL_ERROR_SYSCALL:
  507.             case SSL_ERROR_SSL:
  508.                errors = 1, dobreak = 1;
  509.                break;
  510.  
  511.             case SSL_ERROR_ZERO_RETURN:
  512.                errors = 1, dobreak = 1;
  513.                break;
  514.          }
  515.  
  516.          if (dobreak == 1)
  517.          {
  518.             dobreak = 0;
  519.             break;
  520.          }
  521.  
  522. #        else /* normal error handling */
  523.          if (res <= 0)
  524.          {
  525.             if ((res == ERROR) && (errno == EINTR)) continue;
  526.  
  527.             errors = 1;
  528.             break;
  529.          }
  530.  
  531.          else if (res < size)
  532.          {
  533.             lnptr += res, size -= res;
  534.             continue;
  535.          }
  536.  
  537.          else break;
  538. #        endif /* SSL/NOSSL error handling */
  539.       }
  540.  
  541.       if (errors != 1)
  542.       {
  543.          logmsg.cmdlen = importInt(lnptr);
  544.  
  545.          if (logmsg.cmdlen > len - 1)
  546.          {
  547.             error("(in recv_data) cmdlen > buflen.. truncating\n\n");
  548.             logmsg.cmdlen = len - 1;
  549.          }
  550.  
  551.          size = logmsg.cmdlen;
  552.          while(1)
  553.          {
  554. #           ifndef NOSSL
  555.             res = SSL_read(sslconn, cmdptr, (u_int)size);
  556. #           else
  557.             res = read(sockfd, cmdptr, size);
  558. #           endif
  559.  
  560.             /* ------------------ */
  561.  
  562. #           ifndef NOSSL /* SSL error handling */
  563.             sslerr = SSL_get_error(sslconn, res);
  564.             switch (sslerr)
  565.             {
  566.                case SSL_ERROR_NONE:
  567.                   if (size == res)
  568.                   {
  569.                      dobreak = 1;
  570.                      break;
  571.                   }
  572.  
  573.                   cmdptr += res, size -= res;
  574.                   break;
  575.  
  576.                case SSL_ERROR_WANT_WRITE:
  577.                case SSL_ERROR_WANT_READ:
  578.                case SSL_ERROR_WANT_X509_LOOKUP:
  579.                   errors = 1, dobreak = 1;
  580.                   break;
  581.  
  582.                case SSL_ERROR_SYSCALL:
  583.                case SSL_ERROR_SSL:
  584.                   errors = 1, dobreak = 1;
  585.                   break;
  586.  
  587.                case SSL_ERROR_ZERO_RETURN:
  588.                   errors = 1, dobreak = 1;
  589.                   break;
  590.             }
  591.  
  592.             if (dobreak == 1)
  593.             {
  594.                dobreak = 0;
  595.                break;
  596.             }
  597.  
  598. #           else /* normal error handling */
  599.             if (res <= 0)
  600.             {
  601.                if ((res == ERROR) && (errno == EINTR)) continue;
  602.  
  603.                errors = 1;
  604.                break;
  605.             }
  606.  
  607.             else if (res < size)
  608.             {
  609.                cmdptr += res, size -= res;
  610.                continue;
  611.             }
  612.  
  613.             else break;
  614. #           endif /* SSL/NOSSL error handling */
  615.          }
  616.       }
  617.  
  618. #     ifndef NOSSL /* SSL error handling */
  619.       switch (sslerr)
  620.       {
  621.          case SSL_ERROR_NONE:
  622.             /* we don't need to do anything for this */
  623.             break;
  624.  
  625.          case SSL_ERROR_WANT_WRITE:
  626.          case SSL_ERROR_WANT_READ:
  627.          case SSL_ERROR_WANT_X509_LOOKUP:
  628.             error("got SSL_ERROR_WANT_(WRITE/READ/X509_LOOKUP)..ignoring\n\n");
  629.             errors = 0;
  630.  
  631.             break;
  632.  
  633.          case SSL_ERROR_SYSCALL:
  634.          case SSL_ERROR_SSL:
  635.             if (errno <= 0)
  636.             {
  637.                errors = 0;
  638.                break;
  639.             }
  640.  
  641.             error("error with SSL_read: %s\n\n", strerror(errno));
  642.  
  643.             ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  644.             if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  645.  
  646.             debug("now restarting..\n");
  647.  
  648.             if (done == 1) return;
  649.  
  650.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  651.             else longjmp(infoconn, 1);
  652.  
  653.          case SSL_ERROR_ZERO_RETURN:
  654.             error("server disconnected.. now restarting\n\n");
  655.  
  656.             if (done == 1) return;
  657.  
  658.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  659.             else longjmp(infoconn, 1);
  660.       }
  661.  
  662. #     else /* normal error handling */
  663.       if (res <= 0)
  664.       {
  665.          if (done == 1)
  666.          {
  667.             errors = 1;
  668.             return;
  669.          }
  670.  
  671.          if (res == 0)
  672.          {
  673.             signal(SIGPIPE, SIG_IGN);
  674.  
  675.             connected = 0;
  676.             if (silent != 1)
  677.             {
  678.                if (child == 1)
  679.                   error("(in parent) server disconnected... restarting\n\n");
  680.  
  681.                else
  682.                   error("server disconnected... restarting\n\n");
  683.             }
  684.  
  685.             if (done == 1) return;
  686.  
  687.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  688.             else longjmp(infoconn, 1);
  689.          }
  690.  
  691.          else if (errno > 0)
  692.          {         
  693.             if (child == 1)
  694.             {
  695. #              ifndef NOSSL
  696.                ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  697.                if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  698.  
  699. #              else
  700.                error("(in parent) "
  701.                      "error receiving data from the server: %s\n\n",
  702.                      (ch == 1 ? "child" : "parent"), strerror(errno));
  703. #              endif
  704.             }
  705.  
  706.             else
  707.             {
  708. #              ifndef NOSSL
  709.                ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  710.                if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  711.  
  712. #              else
  713.                error("error receiving data from the server: %s\n\n",
  714.                      strerror(errno));
  715. #              endif
  716.             }
  717.  
  718.             signal(SIGPIPE, SIG_IGN);
  719.  
  720.             if (done == 1) return;
  721.  
  722.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  723.             else longjmp(infoconn, 1);
  724.          }
  725.  
  726.          else if ((gotAlrm == 1) || (errors == 1))
  727.          {
  728.             errors = 1, gotAlrm = 0;
  729.  
  730.             if (silent != 1)
  731.             {
  732. #              ifndef NOSSL
  733.                ERR_print_errors_fp(stderr), ERR_print_errors_fp(errlogfd1);
  734.                if (debugging == 1) ERR_print_errors_fp(dblogfd1);
  735.  
  736. #              else
  737.                error("(in recv_data) error or timeout while in read()\n\n");
  738. #              endif
  739.             }
  740.  
  741.             if (gotInfoServ == 1) 
  742.             {
  743.                if (done == 1) return;
  744.  
  745.                if (silent != 1)
  746.                   error("now reconnecting to a new streaming server\n\n");
  747.  
  748.                longjmp(reconnect, 1);
  749.             }
  750.  
  751.             else
  752.             {
  753.                if (done == 1) return;
  754.  
  755.                if (silent != 1)
  756.                   error("now reconnecting to a new info server\n\n");
  757.  
  758.                longjmp(infoconn, 1);
  759.             }
  760.          }
  761.       }
  762. #     endif /* SSL/NOSSL error handling */
  763.  
  764.       if ((res == 0) || (((readbuf[0] == '\0') || (readbuf[0] == '\n')) &&
  765.           (isprint((int)readbuf[1]) == 0)))
  766.       {
  767. #        ifdef NOSSL
  768.          if (silent != 1)
  769.          {
  770.             if (child == 1)
  771.                debug("[in recv_data] (in parent)\n"
  772.                      "data received was bad.. returning\n\n");
  773.  
  774.             else
  775.                debug("(in recv_data) data received was bad.. returning\n\n");
  776.  
  777.             debug("bad data is:\n%s\n", readbuf);
  778.          }
  779.  
  780. #        else
  781.          error("server possibly disconnected... restarting\n\n");
  782.  
  783.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  784.          else longjmp(infoconn, 1);
  785. #        endif
  786.  
  787.          return;
  788.       }
  789.  
  790.       if (silent != 1)
  791.       {
  792.          if (child == 1) debug("(in parent) data received from server..\n");
  793.          else debug("data received from server..\n");
  794.  
  795.          if (strlen(readbuf) <= 44)
  796.             debug("the data is: %s%c", readbuf,
  797.                   (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  798.  
  799.          else
  800.             debug("the data is:\n%s%c", readbuf,
  801.                   (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  802.       }
  803.  
  804. /*
  805.       if (strncmp(readbuf, "OKAY", 4) != 0)
  806.       {
  807.          * (void)sleep(1); *
  808.          * silent = 1, * send_data("OKAY"), silent = 0;
  809.  
  810.          if ((errors == 1) || (gotAlrm == 1))
  811.          {
  812.             (void)signal(SIGPIPE, SIG_IGN);
  813.             
  814.             errors = 1, gotAlrm = 0;
  815.             if (done == 1) return;
  816.  
  817.             error("(in recv_data) error in send_data().. restarting\n\n");
  818.             if (gotInfoServ == 1) longjmp(reconnect, 1);
  819.             else longjmp(infoconn, 1);
  820.          }
  821.       }
  822.  
  823.       else  
  824. */    if (strncmp(readbuf, "ERROR", 5) == 0)
  825.       {
  826.          signal(SIGPIPE, SIG_IGN);
  827.  
  828.          error("ERROR received from the server..\n"
  829.                "the message was: %s\n", readbuf);
  830.  
  831.          if (done == 1) return;
  832.  
  833.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  834.          else longjmp(infoconn, 1);
  835.       }
  836.  
  837.       else if (strncmp(readbuf, "CHNGSERV", 8) == 0)
  838.       {
  839.          signal(SIGPIPE, SIG_IGN);
  840.          error("server has requested 'chngserv' cmd.. restarting\n\n");
  841.  
  842.          if (done == 1) return;
  843.  
  844.          if (gotInfoServ == 1) longjmp(reconnect, 1);
  845.          else longjmp(infoconn, 1);
  846.       }
  847.  
  848.       /* junk.. just to make pretty */
  849.       else if ((debugging == 1) &&
  850.                (((strncmp(readbuf, "PING", 4) == 0)  &&
  851.                (strncmp(readbuf, "CHECK", 5) != 0)) ||  
  852.                (strncmp(readbuf, "OKAY", 4) == 0)))
  853.       {
  854.          if ((debugging == 1) && (silent != 1))
  855.          {
  856.             (void)putchar('\n');
  857.             (void)write(dblogfd, "\n", 1);
  858.          }
  859.       }
  860.  
  861.       if (child == 0) return;  /* we don't need to do this if there */
  862.                                /* is no child to notify...          */
  863.       
  864.       if (oldbuf[0] == '\0') 
  865.       {
  866.          if (silent != 1)
  867.          {
  868.             if (child == 1)
  869.                debug("(in parent) no data in oldbuf.. putting it in\n");
  870.  
  871.             else debug("no data in oldbuf.. putting it in\n");
  872.          }
  873.  
  874.          strncpy(oldbuf, readbuf, sizeof(oldbuf));
  875.          newdata = 1;
  876.       }
  877.  
  878.       if (((strcmp(oldbuf, readbuf) != 0) || (newdata == 1)) &&
  879.           ((strncmp(readbuf, "PING", 4) == 0) || 
  880.           (strncmp(readbuf, "OKAY", 4) == 0)))
  881.       {
  882.          /* new data has been receiveed */
  883.       
  884.          if (running == 0)
  885.          { 
  886.             if (silent != 1)
  887.             {
  888.                if ((debugging == 1) && (silent != 1))
  889.                {
  890.                   (void)putchar('\n');
  891.                   (void)write(dblogfd, "\n", 1);
  892.                }
  893.  
  894.                debug("running = 0, waiting..\n");
  895.             }
  896.  
  897.             while (running == 0) (void)sleep(1);
  898.          }
  899.  
  900.          if (silent != 1)
  901.             debug("(in parent) got new data... sending SIGUSR1 to child\n\n");
  902.  
  903.          memset(segptr, 0, MAXSEGSIZE);
  904.          
  905.          /* put it in shared mem */
  906.          (void)strncpy(segptr, readbuf, MAXSEGSIZE-1);
  907.  
  908.          if (silent != 1) 
  909.             debug("shared segment data = %s%c", segptr,
  910.                   (strchr(segptr, '\n') == NULL ? '\n' : '\0'));   
  911.       
  912.          running = 0;         
  913.          (void)signal(SIGUSR2, startUp);
  914.  
  915. #       if !defined(SUN) && !defined(BSD)
  916. #        ifdef _POSIX_SAVED_IDS
  917.          res = setuid(0);
  918. #        else
  919.          res = seteuid(0);
  920. #        endif
  921.  
  922.          if (res == ERROR)
  923.          {
  924.             error("error setting [e]uid: %s\n\n", strerror(errno));
  925.             quit(ERROR);
  926.          }
  927. #       endif
  928.  
  929.          /* notify child there is new data */
  930.          res = kill(chpid, SIGUSR1);
  931.          if (res == ERROR)
  932.          {
  933.             error("error killing the child process: %s\n\n",
  934.                   strerror(errno));
  935.  
  936. #           if !defined(SUN) && !defined(BSD)
  937.             if (pwd != NULL)
  938.             {
  939. #              ifdef _POSIX_SAVED_IDS
  940.                res = setuid(pwd->pw_uid);
  941. #              else
  942.                res = seteuid(pwd->pw_uid);
  943. #              endif
  944.  
  945.                if (res == ERROR)
  946.                {
  947.                   error("error setting [e]uid: %s\n\n", strerror(errno));
  948.                   quit(ERROR);
  949.                }
  950.             }
  951. #           endif
  952.  
  953.             if (done != 1)
  954.             {
  955.                /* if something went wrong here.. forget pinging */
  956.                child = 0, pinging = 0;
  957.                if (done == 1) return;
  958.  
  959.                if (gotInfoServ == 1) longjmp(reconnect, 1);
  960.                else longjmp(infoconn, 1);
  961.             }
  962.          }
  963.  
  964. #        if !defined(SUN) && !defined(BSD)
  965.          if (pwd != NULL)
  966.          {
  967. #           ifdef _POSIX_SAVED_IDS
  968.             res = setuid(pwd->pw_uid);
  969. #           else
  970.             res = seteuid(pwd->pw_uid);
  971. #           endif
  972.  
  973.             if (res == ERROR)
  974.             {
  975.                error("error setting [e]uid: %s\n\n", strerror(errno));
  976.                quit(ERROR);
  977.             }
  978.          }
  979. #        endif
  980.  
  981.          if ((debugging == 1) && (silent != 1))
  982.          {
  983.             (void)putchar('\n');
  984.             (void)write(dblogfd, "\n", 1);
  985.          }
  986.  
  987.          /* while (running == 0) sleep(1); */
  988.          return;
  989.       }
  990.    }
  991.  
  992.    else /* if (ch == 1) */
  993.    {
  994.       errno = 0;
  995.  
  996.       if ((debugging == 1) && (silent != 1))
  997.       {
  998.          (void)putchar('\n');
  999.          (void)write(dblogfd, "\n", 1);
  1000.       }
  1001.  
  1002.       if (newData == 0)
  1003.       {
  1004.          if (silent != 1) debug("newData = 0, waiting..\n");
  1005.          while (newData == 0) (void)sleep(1);
  1006.       }
  1007.  
  1008.       newData = 0; /* FIX - race condition.. block SIGUSR1.. */
  1009.  
  1010.       /* make sure buffer is blank */
  1011.       memset(readbuf, 0, len);
  1012.     
  1013.       if (silent == 1) debug("shared segment data = %s\n", segptr);
  1014.       memcpy(readbuf, segptr, len), readbuf[len] = '\0';
  1015.      
  1016.       if (silent != 1) debug("readbuf (after copying seg) = %s\n", readbuf);
  1017.  
  1018.       newData = 0; /* reset data marker */
  1019.  
  1020.       if (silent != 1) debug("(in child) sending parent SIGUSR2 now\n\n");
  1021.  
  1022. #    if !defined(SUN) && !defined(BSD)
  1023. #     ifdef _POSIX_SAVED_IDS
  1024.       res = setuid(0);
  1025. #     else
  1026.       res = seteuid(0);
  1027. #     endif
  1028.  
  1029.       if (res == ERROR)
  1030.       {
  1031.          error("error setting [e]uid: %s\n\n", strerror(errno));
  1032.          quit(ERROR);
  1033.       }
  1034. #    endif
  1035.  
  1036.       /* tell parent we got data.. it can continue */
  1037.       res = kill(getppid(), SIGUSR2);
  1038.       if (res == ERROR)
  1039.       {
  1040.          error("(in child) error killing the parent process: %s\n\n",
  1041.                strerror(errno));  
  1042.  
  1043.          if (done != 1) quit(ERROR);
  1044.       }
  1045.  
  1046.       if (pwd != NULL)
  1047.       {
  1048. #       if !defined(SUN) && !defined(BSD)
  1049. #        ifdef _POSIX_SAVED_IDS
  1050.          res = setuid(pwd->pw_uid);
  1051. #        else
  1052.          res = seteuid(pwd->pw_uid);
  1053. #        endif
  1054.  
  1055.          if (res == ERROR)
  1056.          {
  1057.             error("error setting [e]uid: %s\n\n", strerror(errno));
  1058.             quit(ERROR);
  1059.          }
  1060. #       endif
  1061.       }
  1062.  
  1063.       return;
  1064.    }
  1065. }
  1066.